class Node(object):
    def __init__(self, val=" ", left=None, right=None):
        self.val = val
        self.left = None
        self.right = None


import collections


class Solution(object):
    def checkEquivalence(self, root1, root2):
        p1 = collections.defaultdict(int)
        p2 = collections.defaultdict(int)

        def dfs(node, p):
            if node.left:
                dfs(node.left, p)
            if node.right:
                dfs(node.right, p)
            if not node.left and not node.right:
                p[node.val] += 1

        dfs(root1, p1)
        dfs(root2, p2)

        for key in p1:
            if p1[key] != p2[key]:
                return False
        return True
